/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "TerrainDecal.h"

namespace PQEngine
{
	const Ogre::String TerrainDecal::TERRAIN_DECAL_TEXTURE="TerrainDecalTexture";
	TerrainMaterialGeneratorWithDecal::TerrainMaterialGeneratorWithDecal(Ogre::Frustum *frustum) 
	{
		assert(frustum != 0);
		_frustum = frustum;
	}

	Ogre::MaterialPtr TerrainMaterialGeneratorWithDecal::generate(const Ogre::Terrain* terrain)
	{
		//first invoke parent method
		Ogre::MaterialPtr materialPtr = Ogre::TerrainMaterialGenerator::generate(terrain);
		if(!materialPtr.isNull()){
			Ogre::Technique *t = materialPtr->getTechnique(0);

			Ogre::Pass* p = t->createPass();
			p->setName("TerrainDecalPass");
			p->setLightingEnabled(false);
			p->setAmbient(0,0,0);
			p->setSpecular(0,0,0,0);
			p->setSelfIllumination(0.9f,0.9f,0.9f);
			p->setSceneBlending(Ogre::SBT_TRANSPARENT_COLOUR);
			p->setDepthBias(2.5f, 2.5f);

			// create decal texture unit state
			Ogre::TextureUnitState* texState = p->createTextureUnitState(TerrainDecal::TERRAIN_DECAL_TEXTURE);
			texState->setProjectiveTexturing(true, _frustum);
			texState->setTextureAddressingMode(Ogre::TextureUnitState::TAM_BORDER);
			texState->setTextureBorderColour(Ogre::ColourValue(0,0,0,0));
			texState->setTextureFiltering(Ogre::FO_POINT, Ogre::FO_LINEAR, Ogre::FO_NONE);
		}

		return materialPtr;
	}

}
